<template>
	<view class="login-container">

		<view class="wechatapp">
			<image class="m-0-a" style="width: 190rpx;height: 190rpx;" :src="src"></image>
		</view>

		<view>

			<view class="login-btn">
				<button class="btn-normal" v-if="step == 1" @click="clickUserAgreement()">手机号一键登录</button>
				<button class="btn-normal" v-if="step == 2" open-type="getPhoneNumber"
					@getphonenumber="getPhoneNumber">手机号一键登录</button>
			</view>

			<view class="no-login-btn">
				<button class="btn-normal" @click="onNotLogin">暂不登录</button>
			</view>

			<view class="agreement">
				<checkbox-group @change="checkboxChange">
					<label>
						<checkbox value="同意" style="font-size: 22rpx;"><text>我已阅读并同意</text>
							<text @click.stop="showAgreement = true" style="color: #fcbd71;">友趣UQU用户服务协议、隐私政策</text>
						</checkbox>
					</label>
				</checkbox-group>
			</view>
		</view>



		<u-popup :show="showAgreement" mode="bottom" @close="closeAgreement">
			<scroll-view class="user_agreement" scroll-y="true">
				<u-parse :content="user_agreement"></u-parse>
			</scroll-view>
		</u-popup>
	</view>
</template>

<script>
	import * as UserApi from '@/api/user.js';
	import * as MemberApi from '@/api/member.js'
	export default {
		data() {
			return {
				src: 'https://youqu.arrow.enpingshi.com/uploadfiles/logo.jpg',
				step: 1,
				agreement: [],
				showAgreement: false,
				user_agreement: '',
				code: ''
			}
		},
		onShow() {

		},
		onLoad(e) {
			let that = this;
			wx.login({
				success(res) {
					that.code = res.code
					console.log(that.code)
				}
			})
			this.member_setting()
		},
		methods: {
			checkboxChange: function(e) {
				this.agreement = e.detail.value
				if (this.agreement.length > 0) {
					this.step = 2
				} else {
					this.step = 1
				}

			},
			onNotLogin: function() {
				let currentPagesList = getCurrentPages()
				if (currentPagesList.length <= 1) {
					uni.reLaunch({
						url: '/pages/user/user'
					})
				} else {
					uni.navigateBack();
				}
			},
			closeAgreement() {
				this.showAgreement = false
			},
			clickUserAgreement() {
				uni.$u.toast('请阅读并勾选用户协议')
			},
			member_setting() {
				MemberApi.setting().then(res => {
					this.user_agreement = res.data.agreement
				})
			},

			getPhoneNumber(e) {
				var self = this;
				if (e.detail.errMsg !== 'getPhoneNumber:ok') {
					return false;
				}
				uni.showLoading({
					title: "正在处理",
					mask: true
				});

				UserApi.phoneLogin({
					code: self.code,
					phone_code: e.detail.code
				}).then(res => {
					uni.hideLoading();
					if (res.status == 200) {
						// 执行回调函数
						uni.setStorageSync('user_id', res.user_id);
						self.onNotLogin()
					} else if (res.status == 300) {
						uni.$u.toast(res.info)
					} else {
						uni.$u.toast('登录失败')
					}

				})

			},
		},
	}
</script>

<style>
	.login-container {
		padding: 30rpx;
	}

	.wechatapp {
		padding: 80rpx 0 48rpx;
		border-bottom: 1rpx solid #e3e3e3;
		margin-bottom: 72rpx;
		text-align: center;
	}

	.m-0-a {
		margin: 0 auto;
	}

	.wechatapp .header {
		width: 190rpx;
		height: 190rpx;
		border: 2px solid #fff;
		margin: 0rpx auto 0;
		border-radius: 50%;
		overflow: hidden;
		box-shadow: 1px 0px 5px rgba(50, 50, 50, 0.3);
	}

	.auth-title {
		color: #585858;
		font-size: 34rpx;
		margin-bottom: 40rpx;
	}

	.auth-subtitle {
		color: #888;
		margin-bottom: 88rpx;
		font-size: 28rpx;
	}

	.login-btn {
		padding: 0 20rpx;
	}

	.login-btn button {
		height: 88rpx;
		line-height: 88rpx;
		background: #04be01;
		color: #fff;
		font-size: 30rpx;
		border-radius: 999rpx;
		text-align: center;
	}

	.login-btn button.login-btn-ali {
		height: 88rpx;
		line-height: 88rpx;
		background: #1678ff;
		color: #fff;
		font-size: 30rpx;
		border-radius: 999rpx;
		text-align: center;
	}

	.no-login-btn {
		margin-top: 20rpx;
		padding: 0 20rpx;
	}

	.no-login-btn button {
		height: 88rpx;
		line-height: 88rpx;
		background: #dfdfdf;
		color: #fff;
		font-size: 30rpx;
		border-radius: 999rpx;
		text-align: center;
	}

	.agreement {
		margin-top: 30rpx;
		margin-left: 50rpx;
		margin-bottom: 30rpx;
		font-size: 24rpx;
	}

	.user_agreement {
		height: 820rpx;
		width: 710rpx;
		padding: 20rpx;
	}
</style>